package com.woniuxy.creditsystem.systemmag.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.pagehelper.PageInfo;
import com.woniuxy.creditsystem.common.bean.*;
import com.woniuxy.creditsystem.common.bean.ResultMsg;
import com.woniuxy.creditsystem.systemmag.service.IDataDictionaryService;
import com.woniuxy.creditsystem.systemmag.util.ExportToExcel;
import com.woniuxy.creditsystem.systemmag.util.GetBinaryStream;
import com.woniuxy.creditsystem.systemmag.util.ImportExcel;
import org.apache.commons.io.FileUtils;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.File;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("/data")
public class DataDictionaryController {

    @Resource
    private IDataDictionaryService dataDictionaryServiceImpl;

    /**
     * 分页查询
     * @Author liaojian
     * @Return TableResult
     * */
    @GetMapping(value = "/page",produces = {"application/json;charset=utf-8"})
    public TableResult page(PageRequest request, DataDictionaryInfo data){
        System.out.println(request);
        System.out.println(data);
        //1.得到pageinfo对象
        PageInfo<DataDictionaryInfo> pageInfo = dataDictionaryServiceImpl.findAllByParams(request,data);
        System.out.println(pageInfo.getTotal());
        System.out.println(pageInfo.getList());
        //2.将pageinfo对象中的数据封装到tableResult中
        return new TableResult<DataDictionaryInfo>(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 新增数据
     * @Author liaojian
     * */
    @GetMapping(value = "/save",produces = {"application/json;charset=utf-8"})
    public ResultMsg save(DataDictionaryInfo data){
        ResultMsg resultMsg = new ResultMsg(200,"新增成功",null);
        dataDictionaryServiceImpl.save(data);
        return resultMsg;
    }

    /**
     * 修改数据
     * @Author liaojian
     * */
    @GetMapping(value = "/update",produces = {"application/json;charset=utf-8"})
    public ResultMsg update(DataDictionaryInfo data){
        ResultMsg resultMsg = new ResultMsg(200,"修改成功",null);
        dataDictionaryServiceImpl.update(data);
        return resultMsg;
    }

    /**
     * 删除数据
     * @Author liaojian
     * */
    @GetMapping(value = "/delete",produces = {"application/json;charset=utf-8"})
    public ResultMsg delete(long id){
        ResultMsg resultMsg = new ResultMsg(200,"修改成功",null);
        dataDictionaryServiceImpl.delete(id);
        return  resultMsg;
    }

    /**
     * 数据导入
     * @Author liaojian
     * */
    @RequestMapping(value = "/import",produces = {"application/json;charset=utf-8"})
    public ResultMsg importData(@RequestParam("file") MultipartFile file) throws Exception{
        ResultMsg resultMsg = new ResultMsg(200,"导入成功",null);
        dataDictionaryServiceImpl.saveList(file);
        return resultMsg;
    }

    /**
     * 数据导出
     * @Author liaojian
     * */
    @PostMapping(value = "/export",produces = {"application/json;charset=utf-8"})
    public ResultMsg export(@RequestBody List<DataDictionaryInfo> lists) throws Exception{
        String fileName = ExportToExcel.getFileName(lists);
        ResultMsg resultMsg = new ResultMsg(200,"导出成功",fileName);
        return  resultMsg;
    }

    /**
     * 下载数据
     * @Author liaojian
     * */
    /**
     * 下载
     * @param fileName 文件的名称
     * @param userAgent 浏览器的种类
     * @return 设置响应的实体对象 由于我们现在是返回的图片，所以我们以字节数组的方式来返回
     */
    @RequestMapping("/download")
    public ResponseEntity<byte[]> download(String fileName, @RequestHeader("User-Agent") String userAgent){
        return GetBinaryStream.getStream(fileName,userAgent);
    }
}
